package com.code.petapp.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.code.petapp.activity.FosterActivity;
import com.code.petapp.activity.PostFosterActivity;
import com.code.petapp.activity.VideoActivity;
import com.code.petapp.adapter.FosterAdapter;
import com.code.petapp.bean.FosterBean;
import com.code.petapp.bean.GoodsBean;
import com.code.petapp.bean.HealthBean;
import com.code.petapp.databinding.FragmentFosterBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.FosterUtils;
import com.code.petapp.util.GoodsUtils;
import com.code.petapp.util.HealthUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.rxjava3.annotations.Nullable;

public class FosterFragment extends Fragment implements FosterAdapter.OnItemClickListener {
    private RecyclerView recyclerView;//声明RecyclerView

    private FosterAdapter adapter;
    FragmentFosterBinding binding;
    private List<String> name;
    private List<HealthBean> list;
    private String[] options;
    private boolean isGetData = false;
    private Map<String, Integer> nameToFosterIdMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentFosterBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        recyclerView = binding.recView;
        list = new ArrayList<>();
        setSpinner();
        adapter = new FosterAdapter(getContext(), list);
        adapter.setOnItemClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        binding.btnFoster.setOnClickListener(view1 -> {
            startActivity(new Intent(getActivity(), PostFosterActivity.class));
        });
        return view;
    }
    @Override
    public void onResume() {//每次重进页面都会刷新数据
        if (!isGetData) {
            //   这里可以做网络请求或者需要的数据刷新操作
            setSpinner();
            isGetData = true;
        }
        super.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        isGetData = false;
    }

    private void setSpinner() {
        FosterUtils.selectAll(SPCommonUtils.getInstance().getToken(), new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(@Nullable ArrayList<FosterBean> beanList) {
                name = new ArrayList<>();
                nameToFosterIdMap = new HashMap<>();
                for (FosterBean bean : beanList) {
                    if (!name.contains(bean.getName()) && bean.getStatus().equals("寄养中")) {
                        name.add(bean.getName());
                        nameToFosterIdMap.put(bean.getName(), bean.getId());
                    }
                }
                options = name.toArray(new String[0]);
                ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, options);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                if(options.length>0){
                    binding.spinnerName.setAdapter(adapter);
                }else{
                    binding.linContent.setVisibility(View.GONE);
                    binding.txtNoFoster.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void error(int code, @Nullable String error) {
                Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
            }
        });


// 创建一个 ArrayAdapter 并设置到 Spinner


// 添加监听器获取选择的选项
        binding.spinnerName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // 处理选中的选项
                String selectedOption = options[position];
                load(selectedOption);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // 没有选中任何选项时的逻辑
            }
        });
    }

    private void load(String name) {
        list.clear();
        int fosterId = nameToFosterIdMap.get(name);
        HealthUtils.selectAll(SPCommonUtils.getInstance().getToken(), fosterId, new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(@Nullable ArrayList<HealthBean> beanList) {
                if (beanList.isEmpty()) {
                    binding.linMeg.setVisibility(View.GONE);
                    binding.txtNoData.setVisibility(View.VISIBLE);

                } else {
                    list.addAll(beanList);
                    binding.newBlood.setText(beanList.get(0).getBlood());
                    binding.newEating.setText(beanList.get(0).getEating());
                    binding.newTemper.setText(beanList.get(0).getTemper());
                    binding.newRenal.setText(beanList.get(0).getRenal());
                    binding.linMeg.setVisibility(View.VISIBLE);
                    binding.txtNoData.setVisibility(View.GONE);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void error(int code, @Nullable String error) {
                Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View parent, int position) {
        String video = list.get(position).getVideo();
        if (video != null) {
            Intent intent = new Intent(getActivity(), VideoActivity.class);
            intent.putExtra("video", video);
            startActivity(intent);
        } else {
            Toast.makeText(getActivity(), "糟糕！视频丢失啦", Toast.LENGTH_SHORT).show();
        }
    }
}